
$('.delete-staff').on('click',function(){
	alert(11111)
})

$(function() {

	showTable();
	/**
	 * 添加员工
	 * @param  {[type]} ) {		fadeModal('.add-modal');	} [description]
	 * @return {[type]}   [description]
	 */
	$('.btn-add').on('click', function() {
		fadeModal('.add-modal');
	})

	$('.born-date').fdatepicker();

	$('.add-staff').on('click', function() {

		// console.log(validateDep());
		if (validateStaff() != undefined) {
			$.ajax({
				url: "/staff/",
				data: {
					name: validateStaff().staffName,
					gender: validateStaff().sex,
					birthday: validateStaff().born,
					department: validateStaff().department,
					positionTitles: validateStaff().title,
					position: validateStaff().job,
					phone: validateStaff().phone
				},
				type: "POST",
				dataType: 'json',

				success: function(data) {
					var status = data.status;
					if (status == 500) {
						setAlert('非法操作！');
					} else if (status == 400) {
						setAlert('添加失败！');
					} else if (status == 200) {
						showTable($('.linkPage-input')[0].value);
						setAlert('添加成功');
						$('.add-modal,.mask').fadeOut();

					} else {
						setAlert('系统繁忙，请刷新重试');
					}
				},
				error: function() {
					setAlert('系统繁忙，请刷新重试');
				}

			});

		}

	});

/**
 * 批量导入员工
 * @param  {String} ) {		fadeModal('.import-modal');		document.getElementById('filename').value [description]
 * @return {[type]}   [description]
 */
	$('.btn-import').on('click', function() {
		fadeModal('.import-modal');
		document.getElementById('filename').value = "";
	})


	$('.btn-confirm').on('click', function() {

		var filename = document.getElementById('filename').value;

		if (filename == '' || filename == undefined || filename == null) {
			setAlert('请选择文件');

		} else {
			$('.import-modal,.mask').fadeOut();
		}
	})


	
	/**
	 * 弹出修改信息模态框
	 * 填充修改信息模态框
	 * @param  {[type]} ) {				fadeModal('.information-modal');		$(this).parents("tr").first().addClass('selected');		$('.staffNo').val($('.selected').children('td').eq(1).text());		$('.staffName').val($('.selected').children('td').eq(2).text());		$('.sex').val($('.selected').children('td').eq(3).text());		$('.born-date').val($('.selected').children('td').eq(4).text());		$('.department').val($('.selected').children('td').eq(5).text());		$('.title').val($('.selected').children('td').eq(6).text());		$('.job').val($('.selected').children('td').eq(7).text());		$('.phone').val($('.selected').children('td').eq(8).text());		console.log($(this).children('input').val());		$('.thisID-input').val($(this).children('input').val());	} [description]
	 * @return {[type]}   [description]
	 */
		$(document).on('click', '.modify-info', function() {
		//$('.modify-info').on('click', function() {
		fadeModal('.information-modal');
		$(this).parents("tr").first().addClass('selected');
		$('.staffNo').val($('.selected').children('td').eq(1).text());
		$('.staffName').val($('.selected').children('td').eq(2).text());
		$('.sex').val($('.selected').children('td').eq(3).text());
		$('.born-date').val($('.selected').children('td').eq(4).text());
		$('.department').val($('.selected').children('td').eq(5).text());
		$('.title').val($('.selected').children('td').eq(6).text());
		$('.job').val($('.selected').children('td').eq(7).text());
		$('.phone').val($('.selected').children('td').eq(8).text());
		console.log($(this).children('input').val());
		$('.thisID-input').val($(this).children('input').val());

	})

	/**
	 * [提交修改后员工信息]
	 * @param  {[type]} )         {		var        thisId        [description]
	 * @param  {[type]} type:     "PUT"          [description]
	 * @param  {[type]} dataType: 'json'         [description]
	 * @param  {[type]} success:  function(data) {					var    status        [description]
	 * @return {[type]}           [description]
	 */
	$('.btn-modify').on('click', function() {

		var thisId = $('.thisID-input').val();
		if (validateModifyStaff()!=undefined) {

			$.ajax({
				url: "/staffId/" + $('.staff-modify').val(),
				data: {
					id: $('staff-modify').val(),
					name: $('selected').children('staff-info-name').val(),
					gender: $('selected').children('staff-info-gender').val(),
					birthday: $('selected').children('staff-info-age').val(),
					department: $('selected').children('staff-info-depa').val(),
					positionTitles: $('selected').children('staff-title').val(),
					position: $('selected').children('staff-info-job').val(),
					phone: $('selected').children('staff-info-phone').val()
				},

				type: "PUT",
				dataType: 'json',
				success: function(data) {
					var status = data.status;
					if (status == 500) {
						setAlert('非法操作！');
					} else if (status == 400) {
						setAlert('修改失败！');
					} else if (status == 200) {
						showTable($('.linkPage-input')[0].value);
						setAlert("修改成功！");
						$('.information-modal,.mask').fadeOut();
					} else {
						setAlert('系统繁忙，请刷新重试');
					}
				},
				error: function() {
					setAlert('系统繁忙，请刷新重试');
				}
			})
		}


	})

});

/**
 * [validateModifyStaff 验证修改后的员工信息]
 * @return {[object]} [员工信息]
 */
function validateModifyStaff() {
	var staffName = $('.form-control .staffName').val(), //员工姓名
		sex = $('.form-control .sex2').val(), //性别
		born = $('.form-control .born-date').val(), //出生年月
		department = $('.form-control .department').val(),
		// depDate = $('.dep-date').val(),//创建日期   
		title = $('.form-control .title').val(), //职称
		job = $('.form-control .job').val(),
		phone = $('.form-control .phone').val(); //电话; //职位
	if (staffName == '') {
		setAlert('请填写员工姓名');
	} else if (staffName.length > 20) {
		setAlert('员工姓名过长，请重新输入');
	} else if (department.length > 20) {
		setAlert('部门名过长，请重新输入');
	} else if (!(/^1[34578]\d{9}$/.test(phone))) {
		setAlert('请输入合法的手机号');
	} else if (title.length > 20) {
		setAlert('职称过长，请重新输入');
	} else if (job.length > 20) {
		setAlert('职位过长，请重新输入');
	} else {
		return {
			staffName: staffName,
			sex: sex,
			birthday: born,
			department: department,
			phone: phone,
			positionalTitles: title,
			position: job
		}
	}
}

/**
 * [validateStaff 添加员工信息验证]
 * @param {[type]} [varname] [description]
 * @return {[object]} [员工信息]
 */
function validateStaff() {
	var staffName = $('.form-control .staffName2').val(), //员工姓名
		sex = $('.form-control .sex2').val(), //性别
		born = $('.form-control .born-date2').val(), //出生年月
		department = $('.form-control .department2').val(),
		// depDate = $('.dep-date').val(),//创建日期   
		title = $('.form-control .title2').val(), //职称
		job = $('.form-control .job2').val(),
		phone = $('.form-control .phone2').val(); //电话; //职位
	if (staffName == '') {
		setAlert('请填写员工姓名');
	} else if (staffName.length > 20) {
		setAlert('员工姓名过长，请重新输入');
	} else if (department.length > 20) {
		setAlert('部门名过长，请重新输入');
	} else if (!(/^1[34578]\d{9}$/.test(phone))) {
		setAlert('请输入合法的手机号');
	} else if (title.length > 20) {
		setAlert('职称过长，请重新输入');
	} else if (job.length > 20) {
		setAlert('职位过长，请重新输入');
	} else {
		return {
			staffName: staffName,
			sex: sex,
			birthday: born,
			department: department,
			phone: phone,
			positionalTitles: title,
			position: job
		}
	}
}



/**
 * 获取文件上传路径名
 * @param  {[type]} th [description]
 * @return {[type]}    [description]
 */
function changeText(th) {
	var filename = document.getElementById("filename");
	var exist = th.value.lastIndexOf("\\");
	// 火狐浏览器直接可以获取到文件名
	if (exist == -1) {
		filename.value = th.value;
	}
	//chrome或获取到绝对路径
	else {
		filename.value = th.value.substring(exist + 1);
	}
}

$('.file-btn').click(function() {
	$('.file-hide').click();
})



/**
 * 刷新表格
 * @param  {[type]} pageNum [description]
 * @param  {[type]} records [description]
 * @return {[type]}         [description]
 */
function showTable(pageNum, records) {
	if (records == 'undefined') {
		records = 20;
	}

	$.ajax({
		url: "../../../assets/json/staff.json",
		type: "POST",
		dataType: 'json',
		success: function(data) {
			console.log(data);
			var myTemplate = Handlebars.compile($("#view-template").html());
			console.log(myTemplate(data))
			$('.view-line').html(myTemplate(data));
		},
		error: function() {
			setAlert('系统繁忙，请刷新重试');
		}
	});

}


$('.upPage').click(function() {
	// $('.thisID-input').value = 1;
	$('.linkPage-input')[0].value++;

	showTable($('.linkPage-input')[0].value);
})


$('.downPage').click(function() {
	// $('.thisID-input').value = 1;
	if ($('.linkPage-input')[0].value > 1) {

		$('.linkPage-input')[0].value--;
	} else {
		setAlert("已经是第一页了");
	}

	showTable($('.linkPage-input')[0].value);

})


/**
 * 逐一删除员工
 * @param  {[type]} )         {	var         thisId        [description]
 * @param  {[type]} type:     "DELECT"       [description]
 * @param  {[type]} dataType: 'json'         [description]
 * @param  {[type]} success:  function(data) {				var     status        [description]
 * @return {[type]}           [description]
 */
$(document).on('click', '.delete-staff', function() {
	var thisId = $(this).children('input').val();
	console.log(thisId);

	setConfirm('是否确定删除此员工', function() {
		// 
		$.ajax({
			url: "/staffId/" + $('.staff-id-delete').val() + "/" + thisId,
			data: {
				id: $('staff-id-delete').val()
			},
			type: "DELECT",
			dataType: 'json',
			success: function(data) {
				var status = data.status;
				if (status == 500) {
					setAlert('非法操作！');
				} else if (status == 400) {
					setAlert('删除员工失败！');
				} else if (status == 200) {
					/*                    $('tr').removeClass('selected');
					                    $(this).parents('tr').first().addClass('selected');
					                    $('.selected').remove();*/
					showTable($('.linkPage-input')[0].value);
					setAlert('删除员工成功');
				} else {
					setAlert('系统繁忙，请刷新重试');
				}
			},
			error: function() {
				setAlert('系统繁忙，请刷新重试');
			}
		});

	})
})

/**
 * 批量删除员工
 * @param  {[type]} ) {	fadeModal('.confirm-modal');} [description]
 * @return {[type]}   [description]
 */
$('.delete-btn').click(function() {

	fadeModal('.confirm-modal');
})

$(document).on('click', '.confirm-btn', function() {
	//循环遍历勾选中的checkbox
	var input = document.getElementsByTagName('input');
	var thisId = new Array();
	for (var i = 0; i < input.length; i++) {
		if (input[i].type == 'checkbox') {
			if (input[i].checked) {
				thisId[i] = $(this).children('input').val();
			}
		}
	}
	if (thisId.length == 0) {
		setAlert('请勾选要删除的员工');
	} else {

		$.ajax({
			url: "",

			data: {
				id: thisId
			},
			dataType: "json",
			type: "DELETE",
			traditional: true,
			success: function(data) {
				var status = data.status;
				if (status == 500) {
					setAlert('非法操作！');
				} else if (status == 400) {
					setAlert('删除失败！');
				} else if (status == 200) {
					/*                    $('tr').removeClass('selected');
					                    $(this).parents('tr').first().addClass('selected');
					                    $('.selected').remove();*/
					showTable($('.linkPage-input')[0].value);
					setAlert('删除成功');
				} else {
					setAlert('系统繁忙，请刷新重试');
				}
			},
			error: function() {
				setAlert('系统繁忙，请刷新重试');
			}
		})
	}

})


/**
 * 搜索员工
 * @param  {[type]} )         {	$.ajax({		url: "",		data:                 {			name:    ('.search-input').text()		} [description]
 * @param  {[type]} type:     "GET"              [description]
 * @param  {[type]} dataType: 'json'             [description]
 * @param  {[type]} success:  function(data)     {			var                   status        [description]
 * @param  {[type]} error:    function()         {			setAlert('系统繁忙，请刷新重试' [description]
 * @return {[type]}           [description]
 */
$('.search-btn').click(function() {

	$.ajax({
		url: "",
		data: {
			name: ('.search-input').text()
		},
		type: "GET",
		dataType: 'json',
		success: function(data) {
			var status = data.status;
			if (status == 500) {
				setAlert('非法操作！');
			} else if (status == 400) {
				setAlert('查询员工失败！');
			} else if (status == 200) {

				showTable($('.linkPage-input')[0].value);
				setAlert('删除员工成功');
			} else {
				setAlert('系统繁忙，请刷新重试');
			}
		},
		error: function() {
			setAlert('系统繁忙，请刷新重试');
		}
	});


})

/*function showLoginTemplate(){

	$.ajax({
		url: "../../../assets/json/staff.json",
		type: "POST",
		dataType: 'json',
		success: function(data) {
			console.log(data);
			var myTemplate = Handlebars.compile($("#allocate-login-template").html());
			console.log(myTemplate(data))
			$('.modify-login-div').html(myTemplate(data));
		},
		error: function() {
			setAlert('系统繁忙，请刷新重试');
		}
	});

}*/

/*//分配登录权限
$(document).on('click','.allocate-login',function(){
	fadeModal('.allocate-login-modal');
})
*/
